import type { RouteParams, Router, RouteRecordRaw } from "vue-router";
import { PAGE_NOT_FOUND_ROUTE } from "/@/router/routes/error";
import { PageEnum } from "/@/enums/page.enum";
import { usePermissionStoreWithOut } from "/@/store/modules/permission";
import { useUserStoreWithOut } from "/@/store/modules/user";
import { usePageStoreWithOut } from "/@/store/modules/page";
import { startProgress, closeProgress } from "/@/plugins/nprogress";

// const LOGIN_PATH = PageEnum.BASE_LOGIN;

// const whitePathList: PageEnum[] = [LOGIN_PATH];

export function createPermissionGuard(router: Router) {
  const userStore = useUserStoreWithOut();
  const permissionStore = usePermissionStoreWithOut();
  const pageStore = usePageStoreWithOut();
  router.beforeEach(async (to, from, next) => {
    startProgress();
    // console.log(to, from);

    // const token = userStore.getToken;

    // if (whitePathList.includes(to.path as PageEnum)) {
    //   if (to.path === LOGIN_PATH && token) {
    //     try {
    //       await userStore.afterLoginAction();
    //       next((to.query?.redirect as string) || "/");
    //       return;
    //     } catch {}
    //   }
    //   next();
    //   return;
    // }

    // // 如果找不到token
    // if (!token) {
    //   // 去的页面如果不需要校验，则直接通行
    //   if (to.meta.ignoreAuth) {
    //     next();
    //     return;
    //   }

    //   // 重定向到登录，记下当前to.path，以作登录后跳回
    //   const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
    //     path: LOGIN_PATH,
    //     replace: true,
    //   };
    //   if (to.path) {
    //     redirectData.query = {
    //       ...redirectData.query,
    //       redirect: to.path,
    //     };
    //   }
    //   next(redirectData);
    //   return;
    // }

    // 如果未获取过用户信息
    if (userStore.getLastUpdateTime === 0) {
      try {
        await userStore.getUserInfoAction();
      } catch (err) {
        next();
        return;
      }
    }

    if (permissionStore.getIsDynamicAddedRoute) {
      next();
      return;
    }

    const routes = await permissionStore.buildRoutesAction();
    // console.log(routes);
    routes.forEach((route) => {
      router.addRoute(route as unknown as RouteRecordRaw);
    });

    permissionStore.setDynamicAddedRoute(true);

    if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
      // 动态添加路由后，此处应当重定向到fullPath，否则会加载404页面内容
      next({ path: to.fullPath, replace: true, query: to.query });
      return;
    } else {
      const redirectPath = (from.query.redirect || to.path) as string;
      const redirect = decodeURIComponent(redirectPath);
      const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
      next(nextData);
    }
  });

  router.afterEach((to) => {
    if (to.name && pageStore.routerParams[to.name]) {
      to.params = { ...(pageStore.routerParams[to.name] as RouteParams) };
    }
    closeProgress();
    // 进入登录页清空用户信息
    if (to.path === PageEnum.BASE_LOGIN) {
    }
  });
}
